<template>
	<view class="page">
		<u-sticky bg-color="rgb(50, 129, 255)">
			<!-- 只能有一个根元素 -->
			<view class="sticky">
				<!-- 积分信息 -->
				<view class="points-info" v-if="userinfo">
					<view class="current-points">
						<view class="label">累计收益</view>
						<view class="value" style="color: #ffff00;">{{userinfo.totalPointsEarned}}</view>
					</view>
					<view class="earnings-info">
						<view class="item-group">
							<view class="item">
								<view class="label">已提现</view>
								<view class="value already-withdrawn" style="color:#111111;">
									{{userinfo.totalPointsSpent}}
								</view>
							</view>
							<view class="item">
								<view class="label">提现中</view>
								<view class="value pending-withdrawal" style="color:#00ff00;">
									{{userinfo.WithdrawPoints}}
								</view>
							</view>
							<view class="item">
								<view class="label">待提现</view>
								<view class="value pending-withdrawal">{{userinfo.pointsPendingWithdrawal}}</view>
							</view>
							<view class="item" style="float: left;">
								<u-button type="primary" size="mini" shape="round" @click="applyWithdrawal"
									class="apply-button">
									申请提现
								</u-button>
							</view>
						</view>
					</view>
				</view>
			</view>
		</u-sticky>

		<!-- 内容展示 -->
		<view class="reward-list">
			<mescroll-body ref="mescrollRef" safearea @init="mescrollInit" @down="downCallback" @up="upCallback">
				<view class="list-content">
					<view @click="handleDetail(item)" class="reward-item" v-for="(item,index) in dataList" :key="index">
						<view class="item-left">
							<view class="title">{{ item.publicName }}</view>
							<view v-if="item.type==1" style="font-size: 24rpx; color: red;" class="title">本人获得</view>
							<view v-if="item.type==2" style="font-size: 24rpx; color: red;" class="title">裂变获得</view>
							<view v-if="item.type==-1" style="font-size: 24rpx; color: green;" class="title">积分消耗</view>
							<view class="time">{{ item.creatorTime }}</view>
						</view>
						<view>
							<view v-if="item.state==1" style="color:red" class="item-right">+
								{{ item.points }}
							</view>
							<view v-if="item.state==-1" style="color:green" class="item-right">-
								{{ item.points}}
							</view>
							<view style="font-size: 13px; color: #999;" class="title">
								余额：{{item.pointsAfter}}
							</view>
						</view>
					</view>
				</view>
			</mescroll-body>
		</view>
		<!-- 申请提现弹窗 -->
		<u-popup v-model="showWithdrawalPopup" mode="center">
			<view class="withdrawal-popup">
				<view class="popup-title">申请提现</view>
				<u-input v-model="withdrawalAmount" style="min-height:80rpx;" placeholder="请输入提现积分" type="number" />
				<view class="button-group">
					<u-button type="primary" size="medium" @click="submitWithdrawal">提交</u-button>
					<u-button size="medium" @click="showWithdrawalPopup = false">取消</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import config from '@/common/js/config.js'

	export default {

		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		data() {
			return {
				currentTab: 0,
				current: 0,
				userinfo: null,
				dataList: [],

				showWithdrawalPopup: false,
				withdrawalAmount: ''
			};
		},
		onLoad(options) {


			this.init();
		},
		methods: {
			init() {
				this.$helper.ajax({
					url: "/api/Jiushi/Userextend/GetUserInfo",
					method: "GET",
					contentType: "application/json",
					success: res => {

						this.userinfo = res.data.data;

					},
					Error: res => {

					}
				});
			},
			upCallback(page) {

				const userProvider = uni.getStorageSync(config.Global.cache + '_UserProvider');

				let httpData = {
					currentPage: page.num,
					pageSize: page.size,
					userId: userProvider.id,
					enabledMark: this.current,
					sidx: 'CreatorTime',


				}

				this.$helper.ajax({
					url: `/api/Jiushi/Pointsdetails`,
					method: 'GET',
					data: httpData,
					success: res => {
						console.log(res)
						let curPageLen = res.data.data.list.length;
						let totalSize = res.data.data.pagination.total;
						this.mescroll.endBySize(curPageLen, totalSize);
						if (page.num == 1) {
							this.dataList = []
						}
						this.dataList = this.dataList.concat(res.data.data.list);
					}
				});
			},
			changeTab(index) {
				this.currentTab = index;
			},
			applyWithdrawal() {
				this.showWithdrawalPopup = true;
			},
			submitWithdrawal() {
				if (this.withdrawalAmount) {
					console.log(`提交提现金额：${this.withdrawalAmount}`);

					let url = "/api/Jiushi/Pointswithdrawal";
					let method = "POST";

					this.$helper.ajax({
						url: url,
						method: method,
						contentType: "application/json",
						data: {
							points: this.withdrawalAmount
						},
						success: res => {

							this.$helper.msg(res.data.msg, 'success');
							this.init();
							this.mescroll.resetUpScroll();

						}
					});

					this.showWithdrawalPopup = false;
					this.withdrawalAmount = '';
				} else {
					uToast({
						type: 'error',
						message: '请输入提现金额'
					});
				}
			}
		}
	};
</script>

<style scoped>
	.page {
		background: linear-gradient(180deg, rgb(50, 129, 255) 40%, #FFFFFF 100%);
		min-height: 100vh;
		padding-top: var(--status-bar-height, 40rpx);
	}

	.points-info {
		background: rgb(50, 129, 255);
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 30rpx;
		color: white;
		margin-bottom: 20rpx;
	}

	.current-points {
		width: 40%;
		background-color: rgba(255, 255, 255, 0.1);
		padding: 20rpx;
		border-radius: 10rpx;
		box-shadow: 0 0 10rpx rgba(255, 255, 255, 0.3);
	}

	.current-points .label {
		color: rgba(255, 255, 255, 0.7);
		font-size: 28rpx;
		margin-bottom: 10rpx;
	}

	.current-points .value {
		font-size: 60rpx;
		font-weight: bold;
		text-shadow: 0 0 10rpx rgba(255, 255, 255, 0.5);
	}

	.earnings-info {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
		width: 50%;
	}

	.item-group {
		flex-direction: column;
		align-items: flex-end;
	}

	.item {
		display: flex;
		align-items: center;
		margin-bottom: 10rpx;
	}

	.item .label {
		color: rgba(255, 255, 255, 0.7);
		font-size: 28rpx;
		margin-right: 20rpx;
	}

	.already-withdrawn,
	.pending-withdrawal {
		font-size: 28rpx;
	}

	.already-withdrawn {
		color: rgba(255, 255, 255, 0.7);
	}

	.pending-withdrawal {
		color: #ffff00;
		font-weight: bold;
	}

	.apply-button {}

	.tab-bar {
		display: flex;
		background-color: rgba(255, 255, 255, 0.8);
		border-radius: 10rpx;
		margin: 20rpx;
		overflow: hidden;
	}

	.tab-item {
		flex: 1;
		text-align: center;
		padding: 20rpx;
		font-size: 32rpx;
		color: #333;
		cursor: pointer;
	}

	.tab-item.active {
		background-color: rgb(50, 129, 255);
		color: white;
	}

	.reward-list {
		background: rgba(255, 255, 255, 0.9);
		margin: 0 20rpx 20rpx;
		border-radius: 20rpx;
		padding: 30rpx;
		box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.1);
	}

	.reward-item {
		padding: 20rpx 0;
		border-bottom: 1rpx solid #E5E5E5;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.reward-item:last-child {
		border-bottom: none;
	}

	.item-left .title {
		font-size: 32rpx;
		text-align: left;
		margin: 0;
	}

	.item-left .time {
		font-size: 28rpx;
		color: #999;
		margin-top: 10rpx;
	}

	.item-right {
		font-size: 32rpx;
		color: rgb(50, 129, 255);
		text-align: right;
	}

	.withdrawal-popup {
		padding: 30rpx;
		background-color: white;
		border-radius: 10rpx;
	}

	.popup-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
		text-align: center;
	}

	u-input {
		margin-bottom: 20rpx;
	}

	.button-group {
		display: flex;
		justify-content: center;
		gap: 20rpx;
	}
</style>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin-right: 10rpx;
					font-size: 34rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;

				.progressBox {
					width: 150rpx;
					float: right;
				}
			}
		}

		.need-remark {
			color: #666;
			font-size: 28rpx;
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 260rpx;
					height: 190rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				margin-left: 10rpx;

				.title {
					font-size: 28rpx;
					line-height: 45rpx;
				}

				.type {
					margin: 6rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
					text-overflow: -o-ellipsis-lastline;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 3;
					line-clamp: 3;
					-webkit-box-orient: vertical;
				}

				.delivery-time {
					color: #0081ff;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			line-height: 70rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.btnBox {
				width: 150rpx;
				display: flex;
				justify-content: space-between;

				.btn {
					line-height: 52rpx;
					width: 140rpx;
					border-radius: 12rpx;
					border: 2rpx solid $u-tips-color;
					font-size: 26rpx;
					text-align: center;
					color: $u-tips-color;
				}

				.evaluate {
					color: $u-type-primary;
					border-color: $u-type-primary;
				}
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 300rpx;
			border-radius: 50%;
			margin: 0 auto;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, #1cbbb4 0%, #0081ff 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
		background: #f1f1f1;
	}

	.swiper-box {
		flex: 1;
		height: 100vh;
	}

	.swiper-item {
		height: 100%;
	}
</style>